/**
 * Created by Administrator on 2018/6/9.
 */
var dataInit;
// var content;


function employeeListInit($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http) {

  CertusFormAjax.ajax("stscompSelectByAutho", {"areaSelect":"XZZX-YGLB"}, function (res) {
    if (res.data) {
      res.data.forEach(function (data) {
        $("#deptSelectId").append('<option  value="' + data.code + '">' + data.name + '</option>')
      })
      $("#deptSelectId").selectpicker('refresh');
    }
  })

  CertusFormAjax.ajax("stsDeptNameSelectByCompany", {}, function (res) {
    if (res.data) {
      res.data.forEach(function (data) {
        //
        $("#departmentSelectId").append('<option  value="' + data.departmentCode + '">' + data.departmentName + '</option>')
      })
      $("#departmentSelectId").selectpicker('refresh');
    }
  })

  $scope.searchNewUser = function () {
    dataInit.parameters = {
      "companyCode": $("#deptSelectId").val(),
      "departmentCode": $("#departmentSelectId").val(),
      "isOld": $("#isOldId").val(),
      "userName": $("#userNameId").val(),
      "areaSelect":"XZZX-YGLB"
    }
    dataInit.refresh(true);
  }

  $scope.exportNewUser = function(){
      CertusFormAjax.download("userListesDown", "companyCode=" + $("#deptSelectId").val()+"&departmentCode="+$("#departmentSelectId").val()+"&isOld="+$("#isOldId").val()+"&userName="+$("#userNameId").val()+"&areaSelect="+"XZZX-YGLB");
  }


  // $("#departmentSelectId").selectpicker({
  //     'selectedText': 'cat'
  // });

  $scope.companyCodeChange = function (a) {
    CertusFormAjax.ajax("stsDeptNameSelectByCompany", { companyCode: a }, function (res) {
      $scope.departmentSelect = res.data;
    })
  }

  var usersColumns = [{
    id: 'userId',
    title: '员工编号',
    type: 'int',
    width: 80,
    columnClass: 'text-center',
  }, {
    id: 'userName',
    title: '姓名',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
  }, {
    id: 'sex',
    title: '性别',
    type: 'string',
    width: 50,
    hideType: 'sm|xs',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      var content = '';
      if (record.sex == 0) {
        content = "女"
      } else {
        content = "男"
      }

      return content;
    }
  }, {
    id: 'departmentName',
    title: '部门',
    type: 'string',
    columnClass: 'text-center'
  }, {
    id: 'positonName',
    title: '岗位',
    type: 'string',
    columnClass: 'text-center',
  }, {
    id: 'isMarriage',
    title: '婚姻状况',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      var content = '';
      if (record.isMarriage == "0") {
        content = "未婚"
      } else if (record.isMarriage == "1") {
        content = "已婚"
      } else {
        content = "已婚已孕"
      }

      return content;
    }
  }, {
    id: 'email',
    title: '邮箱',
    type: 'string',
    width: 250,
    columnClass: 'text-left',
  }, {
    id: 'onPost',
    title: '在职状态',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      var content = '';
      if (record.onPost == "1") {
        content = "在职"
      } else {
        content = "离职"
      }

      return content;
    }
  }, {
    id: 'enrollTime',
    title: '入职时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
  }, {
    id: 'operation',
    title: '操作',
    width: 130,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
      if (record.isOld == 0) {
        var content = '';
        var editMet = "editPersonInfo(\'" + record.userId + "\')";
        var deleteMet = "deleteUser(\'" + record.userId + "\')";
        var met = {
          'XZZX-YGLBC': editMet,
          // 'RSZX-YGLBD':deleteMet   ,'RSZX-YGLBD%delete'
        }
        content = CertusFormAjax.initOperateAutho($scope.autho, ['XZZX-YGLBC%edit'], met, usersColumns);
        if (record.userId) {
          return content;
        }
      }
      return ' ';
    }
  }, {
    id: 'birthday',
    title: '生日',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs'
  }, {
    id: 'phoneNumber',
    title: '电话号码',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs'
  }, {
    id: 'basedArea',
    title: '常住地',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs'
  }, {
    id: 'weChatId',
    title: '企业微信号',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs'
  }, {
    id: 'leaderName',
    title: '直接领导',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs'
  }, {
      id: 'operateUserName',
      title: '最后一次修改人',
      type: 'string',
      columnClass: 'text-center',
      hideType: 'lg|md|sm|xs'
  }];
  function userList() {
    //首先初始化 所有
    dataInit = CertusJsonAjax.dataInit("userListes", {"areaSelect":"XZZX-YGLB"}, usersColumns, 'usersContainer', 'usersToolBarContainer');
  }
  userList();
}

function editPersonInfo(userId) {
  window.location.href = "#/personalInfo/" + userId;
}

function deleteUser(userId, b) {
  layer.confirm("是否删除此公司成员", {
    title: '提示',
    btn: ['是', '否'] //按钮
  }, function () {
    ajaxCertus("deleteUser", { userId: userId }, function () {
      dataInit.refresh(true);
      layer.closeAll();
    });
  });

  // $(b).parent().parent().remove()

}

function deptSelectRefresh() {
  $('#deptSelectId').val('')
  $("#deptSelectId").selectpicker('refresh');
}

function departmentSelectRefresh() {
  $('#departmentSelectId').val('')
  $('#departmentSelectId').selectpicker('refresh')
}
//